/**
 * 
 */
package TestScenario;

import java.util.Random;

import ptvv.*;
import ptvv.system.*;

/**
 * @author dkz
 * 
 */
public class Ran extends DefPortAutomaton {
	// variables needed for computation

	public Ran() {
		this.setName("Ran");

	}

	public Ran(String name) {
		this.setName(name);
	}

	public void starting() throws InterruptedException {
		//String item = eventQueue.take(); // waiting for start
		// System.out.println(item);
		// logger.log("Waiting");

	}

	public void ready() throws InterruptedException {
		// message = this.inputPorts.get(0).queue.
	}

	public void running() throws InterruptedException {
		Random generator = new Random();
		int r = generator.nextInt(2);
		if (r == 0)
			message = "-1";
		else
			message = "1";
		this.outputPorts.get(0).queue.add(message);

		// System.out.println("sleeping..");
		// System.out.println("awake");
		// this.internalState = InternalStates.Suspended;
	}

	public void suspended() throws InterruptedException {

	}

	public void end() {
		// System.out.println("end");
		logger.log(this.getName() + ": " + message);
		logger.packet(new TracePacket(this.getName(), message));
		for (OutputPort op : outputPorts) {
			op.sendInput();
			op.sendStart();
		}

		// System.out.println(outputPort.destination.internalState);

		// System.out.println(outputPort.destination.isInterrupted());
		// repeatBool = true;
	}

	public void dead() {
		// this.internalState = InternalStates.Start;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
